/*
 * CreepPeachyPinata.java
 *
 * Created on January 4, 2008, 5:27 PM
 *
 * Copyright (C) 2007-2008  Hylke van der Schaaf
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, in version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package hextd.creep;

import hextd.Constants.COLOR;
import hextd.util.Cache;
import hextd.util.Geometry;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
import com.usoog.tdcore.gamegrid.ScaledCanvas;
import com.usoog.tdcore.player.Player;

/**
 *
 * @author hylke
 */
public class CreepPeachyPinata extends CreepBase {

	public static String type = "pep";
	//
	private int prevPaintTime;
	private Color bodyColor = new Color(255, 218, 185);
	private Color bodyShellColor = Color.WHITE;
	private Stroke bodyStroke;
	private Stroke bodyShellStroke;
	private Shape[] bodyShapes;
	private Shape bodyShellShape;
	private int bodyStep = 0;
	private static float reductionFactor = 0.005f;
	private static String infoText =
			"<h1>Peachy Piñata</h1>"
			+ "<p>"
			+ "There's a bonus inside, but this piñata is not made out of paper!"
			+ "</p>";

	/**
	 * Creates a new instance of CreepPeachyPinata
	 */
	public CreepPeachyPinata() {
		super("Peachy Piñata");
		this.damageColor = COLOR.OTHER;
		this.damageColorAnti = COLOR.OTHER;
	}

	@Override
	public String getType() {
		return type;
	}

	@Override
	public String getInfoString() {
		return CreepPeachyPinata.infoText
				+ "<p><b>Damage Reduction:</b> " + CreepPeachyPinata.form.format(Math.round(reductionFactor * this.healthMax)) + "<br>"
				+ "" + super.getInfoString() + "</p>";
	}

	@Override
	public void damage(Player shooter, long damage) {
		long realDamage = Math.round(damage - reductionFactor * healthMax);
		realDamage = Math.max(realDamage, 1) * 3 / 4;
		super.damage(shooter, realDamage);
	}

	@Override
	public void resetScale(ScaledCanvas sc) {
		super.resetScale(sc);
		Cache cache = Cache.getInstance();


		if (cache.hasShapeArr("ESPePiShapes" + this.size.letter)) {
			this.bodyShapes = cache.getShapeArr("ESPePiShapes" + this.size.letter);
			this.bodyShellShape = cache.getShape("ESPePiShape" + this.size.letter);
		} else {
			double scale = paintScale * this.size.scaleMod;
			this.bodyShapes = new Shape[30];

			double r0 = scale / 6;
			double dr1 = r0 / 2;
			double dr2 = dr1 / this.bodyShapes.length;
			int max = this.bodyShapes.length;
			for (int i = 0; i < max; i++) {
				Area a = new Area(Geometry.createCircle(0, 0, r0 + max * dr2));
				a.exclusiveOr(new Area(Geometry.createCircle(0, 0, r0 - dr1 * 0 + i * dr2)));
				a.exclusiveOr(new Area(Geometry.createCircle(0, 0, r0 - dr1 * 1 + i * dr2)));
				a.exclusiveOr(new Area(Geometry.createCircle(0, 0, r0 - dr1 * 2 + i * dr2)));
				this.bodyShapes[i] = a;
			}
			this.bodyShellShape = new Area(Geometry.createCircle(0, 0, r0 + max * dr2));
			cache.putShapeArr("ESPePiShapes" + this.size.letter, this.bodyShapes);
			cache.putShape("ESPePiShape" + this.size.letter, this.bodyShellShape);
		}
		this.bodyStroke = new BasicStroke(1f * strokeWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
		this.bodyShellStroke = new BasicStroke(2f * strokeWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
	}

	@Override
	public void paint(Graphics2D g2, int gameTime) {
		if (inactive || dead) {
			prevPaintTime = gameTime;
		} else {
			Stroke defaultStroke = g2.getStroke();
			bodyStep += gameTime - prevPaintTime;
			if (bodyStep >= bodyShapes.length) {
				bodyStep = 0;
			}
			if (bodyStep < 0) {
				bodyStep = 0;
			}
			prevPaintTime = gameTime;

			AffineTransform saveXform = g2.getTransform();
			g2.transform(atTranslate);

			if (shielded) {
				paintShield_bottom(g2);
			}
			paintHealthLine(g2);

			if (bodyShapes[bodyStep] != null) {
				g2.setColor(bodyColor);
				g2.setStroke(bodyStroke);
				Shape shape = bodyShapes[bodyStep];
				g2.draw(shape);
			}
			g2.setColor(bodyShellColor);
			g2.setStroke(bodyShellStroke);
			g2.draw(bodyShellShape);
			g2.setTransform(saveXform);

			if (this.shielded) {
				g2.transform(atTranslate);
				this.paintShield_top(g2);
				g2.setTransform(saveXform);
			}

			super.paintSelected(g2);

			g2.setStroke(defaultStroke);
		}
	}
}
